package com.ls.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 李朔
 * @version 1.0
 */
public class CharacterEncodingFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        String uri=req.getRequestURI();//获得请求路径
        if(uri.endsWith(".css")|| uri.endsWith(".js")){
            //如果是css或者是Js,直接放行
            filterChain.doFilter(servletRequest,servletResponse);
        }

        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html;charset=UTF-8");
        //继续往下走请求

        filterChain.doFilter(servletRequest,servletResponse);

    }

    public void destroy() {

    }
}
